
#include "common.h"

static uint32_t gSysTick = 0UL;

/**
 * @brief  This function is used to get XOR of the DataBuf.
 * @param *pucDataBuf Pointer to the DataBuf
 * @param wDataLength The length of the DataBuf
 * @param ucBase The base value of the DataBuf
 * @retval The XOR of the DataBuf
 */
uint8_t GetXOR(const uint8_t *pubBuf, uint16_t uwLength, uint8_t ubBase)
{
  while (uwLength--)
  {
    ubBase = ubBase ^ *pubBuf++;
  }

  return ubBase;
}

void SysTickInit(uint32_t uiHCLKFrequency, uint32_t uiTicks)
{
  /* Configure the SysTick to have interrupt in 1ms time base */
  SysTick->LOAD = (uint32_t)((uiHCLKFrequency / uiTicks) - 1UL);
  SysTick->VAL  = 0UL;
  SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk | SysTick_CTRL_ENABLE_Msk;
}

void TimeoutInit(void)
{
  gSysTick = 0;
}

uint8_t TimeoutHandle(uint32_t uiNms)
{
  uint8_t ubResult = 0;

  if ((SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk) != 0U)
  {
    gSysTick++;
  }

  if (gSysTick > uiNms)
  {
    ubResult = 1;
  }

  return ubResult;
}

